package dao;

import java.util.ArrayList;
import java.util.Iterator;

import model.calendar.Calendar;

public class Linker
{
	public static ArrayList<model.lectureRoom.LectureRoom> lectureRoomList = new ArrayList<model.lectureRoom.LectureRoom>();
	public static ArrayList<model.student.Group> groupList = new ArrayList<model.student.Group>();
	public static ArrayList<model.teacher.Teacher> teacherList = new ArrayList<model.teacher.Teacher>();
	
	public static ArrayList<model.LectureRoom> lectureRoomRAWList = new ArrayList<model.LectureRoom>();	
	public static ArrayList<model.Group> groupRAWList = new ArrayList<model.Group>();	
	public static ArrayList<model.Teacher> teacherRAWList = new ArrayList<model.Teacher>();
	public static ArrayList<model.Warning> warningRAWList = new ArrayList<model.Warning>();
	
	public static String path = "C:/";
	
	public static CalendarDAO calendarDAO = new CalendarDAO();
	public static LectureRoomDAO lectureRoomDAO = new LectureRoomDAO();
	public static StudentDAO studentDAO = new StudentDAO();
	public static TeacherDAO teacherDAO = new TeacherDAO();
	public static ReservationLRDAO reservationLRDAO = new ReservationLRDAO();
	public static ReservationTDAO reservationTDAO = new ReservationTDAO();
	
	private static boolean sort = true;
	
	public static void sort()
	{
		if(sort)
		{
			sort = false;
			
			for (int i = 0; i < lectureRoomList.size(); i++)
			{
				String link = lectureRoomList.get(i).getLink();
				String id = lectureRoomList.get(i).getId();
				
				for (model.LectureRoom lrRAW : lectureRoomRAWList)
				{
					if(lrRAW.getLink().equals(link))
					{
						lrRAW.setId(id);
					}
				}
			}
			
			for (int i = 0; i < groupList.size(); i++)
			{
				String link = groupList.get(i).getLink();
				String id = groupList.get(i).getId();
				
				for (model.Group gRAW : groupRAWList)
				{
					if(gRAW.getLink().equals(link))
					{
						gRAW.setId(id);
					}
				}
			}
			
			for (int i = 0; i < teacherList.size(); i++)
			{
				String link = teacherList.get(i).getLink();
				String id = teacherList.get(i).getId();
				
				for (model.Teacher tRAW : teacherRAWList)
				{
					if(tRAW.getLink().equals(link))
					{
						tRAW.setId(id);
					}
				}
			}

			for (int i = 0; i < warningRAWList.size(); i++)
			{
				String info = warningRAWList.get(i).getInfo().split("/")[0].trim();
				Iterator<Calendar> calendar = CalendarDAO.getCalendarsCP().values().iterator();
				while(calendar.hasNext())
				{
					Calendar c = calendar.next();
					String cName = c.getName().split("-")[0].replace("/", "").trim();
					if(cName.equals(info))
						warningRAWList.get(i).setId(c.getId());
				}
			}
			
			LectureRoomDAO.serialize();
			TeacherDAO.serialize();
			StudentDAO.serialize();
			ReservationLRDAO.serialize();
			ReservationTDAO.serialize();
			CalendarDAO.serialize();
		}
	}
}
